package binary

import (
	"fmt"
	"reflect"
)

func SetSliceToArray(r *Buffer, val reflect.Value) (bts []byte, n int, err error) {
	alen := val.Len()
	bts = make([]byte, alen)
	if n, err = r.Read(bts); err == nil {
		if n == alen {
			for j := 0; j < alen; j++ {
				if j < len(bts) {
					val.Index(j).SetUint(uint64(bts[j]))
				} else {
					err = fmt.Errorf("len of byte slice is short than array want")
					break
				}
			}
		} else {
			err = fmt.Errorf("short msg")
		}
	}
	return
}
